<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :inline="true">
					<el-form-item label="变更类型：">
						<el-select v-model="state.tableData.param.agentChangeType" placeholder="全部类型" @change="getTableData()">
							<el-option v-for="item in state.agentChangeTypeList" :key="item.id" :label="item.name"
								:value="item.id" />
						</el-select>
					</el-form-item>
					<el-form-item label="选择时间">
						<el-date-picker v-model="state.tableData.param.time" type="daterange" unlink-panels :shortcuts="shortcuts"
							format="YYYY/MM/DD" value-format="YYYY-MM-DD" range-separator="-" :disabled-date="disabledDate"
							start-placeholder="开始日期" end-placeholder="结束日期" />
					</el-form-item>
					<el-form-item label="代理信息:">
						<el-input v-model="state.tableData.param.keyWords" placeholder="姓名、手机、微信昵称搜索" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getTableData()">
							<el-icon>
								<ele-Search />
							</el-icon> 搜 索
						</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex;flex-direction: column;flex: 1;height: inherit;">
				<section style="display: flex;flex-direction: column; flex: 1;height: inherit;">

					<el-table :data="state.tableData.data" v-loading="state.tableData.loading">
						<el-table-column label="个人信息" show-overflow-tooltip>
							<template #default="scope">
								<div>
									<div>
										<el-button text type="primary" @click="onLookInfo(scope.row)">{{ scope.row.agentName
											}}</el-button>
									</div>
									<div>
										{{ scope.row.agentPhone }}
									</div>
								</div>
							</template>
						</el-table-column>

						<el-table-column label="变更类型" show-overflow-tooltip>
							<template #default="scope">
								<div>
									<div>
										{{ scope.row.agentChangeTypeName }}
									</div>
								</div>
							</template>
						</el-table-column>
						<el-table-column label="变更前" show-overflow-tooltip>
							<template #default="scope">
								<div>
									<div v-for="item in scope.row.changeBefore" :key="item">
										{{ item }}
									</div>
								</div>
							</template>
						</el-table-column>
						<el-table-column label="变更后" show-overflow-tooltip>
							<template #default="scope">
								<div>
									<div v-for="item in scope.row.changeAfter" :key="item">
										{{ item }}
									</div>
								</div>
							</template>
						</el-table-column>
						<el-table-column label="操作人" show-overflow-tooltip>
							<template #default="scope">
								<span> {{ scope.row.agentOperaterName }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作人IP" show-overflow-tooltip>
							<template #default="scope">
								<span> {{ scope.row.agentOperaterIp }}</span>
							</template>
						</el-table-column>
						<el-table-column label="说明" show-overflow-tooltip>
							<template #default="scope">
								<span> {{ scope.row.agentChagneDesc }}</span>
							</template>
						</el-table-column>
						<el-table-column label="时间" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ formatDate(new Date(scope.row.createTime), 'YYYY-mm-dd HH:MM:SS')
									}}</span>
							</template>
						</el-table-column>

				
					</el-table>
					<el-row justify="end">
						<el-col :span="24" class="flex-end-center">
							<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
								:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum"
								background v-model:page-size="state.tableData.param.pageSize"
								layout="total, sizes, prev, pager, next, jumper" :total="state.tableData.total">
							</el-pagination>
						</el-col>
					</el-row>
				</section>
			</el-card>
		</div>
		<addSpecsDialog ref="addSpecsDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { getAgentDataChangeList,getAgentDataChangeType } from "/@/api/agent";
import { formatDate } from '/@/utils/formatTime';


// 引入组件
const addSpecsDialog = defineAsyncComponent(() => import('/@/views/commodity/goodspecs/components/addSpecsDialog.vue'));

// 定义变量内容
const addSpecsDialogRef = ref();
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			keywords: "",//姓名、手机、微信昵称搜索
			agentChangeTime: "",
			agentChangeType: "",//变更类型
			pageNum: 1,
			pageSize: 10,
		},
	},
	agentChangeTypeList: []
});
const disabledDate = (time: Date) => time.getTime() > Date.now();

const shortcuts = [
	{
		text: '本月',
		value: () => {
			const end = new Date()
			const start = new Date()
			start.setTime(start.setDate(1))
			return [start, end]
		},
	},
	{
		text: '近一个月',
		value: () => {
			const end = new Date()
			const start = new Date()
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
			return [start, end]
		},
	},
	{
		text: '近三个月',
		value: () => {
			const end = new Date()
			const start = new Date()
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
			return [start, end]
		},
	},
	{
		text: '近半年',
		value: () => {
			const end = new Date()
			const start = new Date()
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 180)
			return [start, end]
		},
	},
	{
		text: '近一年',
		value: () => {
			const end = new Date()
			const start = new Date()
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
			return [start, end]
		},
	},
]
//获取变更类型 
const getChangeType = async () => {
	const { results } = await getAgentDataChangeType();
	state.agentChangeTypeList = results;
}
// 获取商品列表
const getTableData = async () => {
	state.tableData.loading = true;

	let params = <any>{
		keywords: state.tableData.param.keyWords,
		current: state.tableData.param.pageNum,
		size: state.tableData.param.pageSize,
	}
	if (state.tableData.param.agentChangeType != "") {
		params.agentChangeType = state.tableData.param.agentChangeType
	}
	if (state.tableData.param.agentChangeTime != "") {
		params.agentChangeTime = Object.values(state.tableData.param.agentChangeTime)
	}
	const { results } = await getAgentDataChangeList(params);
	state.tableData.loading = false;
	state.tableData.data = results.records;
	state.tableData.total = results.total;
}

const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
const onLookInfo = (row: any) => {

};//查看对应的信息

// 页面加载时
onMounted(() => {
	getChangeType()
	getTableData();
});
</script>

<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';

:deep(.el-card__body) {
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;

	.el-table {
		flex: 1;

		.el-table__inner-wrapper::before {
			background: none !important;
		}
	}
}
</style>
